﻿$(document).ready(function () {
    $.ajaxSetup({
        dataType: 'json',
        cache: false,
        error: function (error) {
            //alert("התרחשה שגיאה: " + error.responseText);
        }
    });

    setDatepicker();

    tableAltRows();

    //when the user submit the TechnicalContact form
    $("#frmTechnicalContact").live("submit", function (e) {
        var contactValues = $(this).serialize();
        $.validator.unobtrusive.parse($(this));

        if ($(this).valid()) {
            var mainDialog = updateDialog("צור קשר", "100", "400");

            //Invoke AJAX method to post the Contact
            $.post(pathPrefix + "Home/TechnicalContact", contactValues, function (result) {
                mainDialog.html(result.Value);

                window.setTimeout(function () {
                    closeDialog();
                }, 3000);
            }, "json");
        }

        e.preventDefault();
    });

    $("#divMainDialog").dialog({
        autoOpen: false,
        title: "כותרת",
        modal: true,
        resizable: false
    });

    $("#divConfirmDialog").dialog({
        autoOpen: false,
        title: "אישור",
        modal: true,
        resizable: false,
        width: 400,
        height: 150
    });
    $("#btnDialogNo").click(function () {
        $("#divConfirmDialog").dialog("close");
    });

    /*$(document).keypress(function (event) {
    var char = event.which;
    var ctrlKey = event.ctrlKey;
    //clicking on CTRL+Z
    if (ctrlKey && (char == 122 || char == 26)) {
    alert();
    }
    });*/
});

function updateDialog(title, height, width) {
    //update the dialog settings
    var mainDialog = $("#divMainDialog");
    mainDialog.dialog("option", "title", title);
    mainDialog.dialog("option", "height", height);
    mainDialog.dialog("option", "width", width);
    mainDialog.dialog("option", "position", "center");

    //show ajax LoadingImage in the dialog
    mainDialog.html("<div class='loadingImage'>");

    mainDialog.dialog("open");

    return mainDialog;
}

function closeDialog() {
    var mainDialog = $("#divMainDialog");
    mainDialog.dialog("close");
}

function openConfirmDialog(message, successFunction) {
    //open the "confirmDialog" and set relevant message to the user
    $("#divConfirmDialog").dialog("open");
    $("#divDialogMessage").html(message);

    $("#btnDialogYes").blur();

    //define the click event of the "Yes" button
    $("#btnDialogYes").unbind("click");
    $("#btnDialogYes").click(successFunction);
    $("#btnDialogYes").click(function () { $("#divConfirmDialog").dialog("close"); });
}

function showContactWindow() {
    var mainDialog = updateDialog("עזרה טכנית", "250", "600");

    //Invoke AJAX method to get the TechnicalContact dialog
    $.get(pathPrefix + "Home/TechnicalContact", null, function (result) {
        mainDialog.html(result);

        if (flashvars != null) {
            //set the album id value
            mainDialog.find("#AlbumId").val(flashvars.albumId);
        }
    }, "html");

    return false;
}

function setDatepicker() {
    //add "datepicker" plugin to all matched elements
    $(".dateTextbox").datepicker({
        isRTL: true,
        dateFormat: 'dd/mm/yy',
        dayNames: ['ראשון', 'שני', 'שלישי', 'רביעי', 'חמישי', 'שישי', 'שבת'],
        dayNamesMin: ['א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ש'],
        monthNames: ['ינואר', 'פברואר', 'מרץ', 'אפריל', 'מאי', 'יוני', 'יולי', 'אוגוסט', 'ספטמבר', 'אוקטובר', 'נובמבר', 'דצמבר']
    });
}

function tableAltRows() {
    // table tr:not(:first-child):nth-child(even)
    $("table tr:not(:first-child):visible:even").addClass("altRow");

    $("table tr:not(:first-child)").hover(function () {
        $(this).addClass("hoverRow");
    }, function () {
        $(this).removeClass("hoverRow");
    });
}

function SliderMovePrevious(sliderPanel, defaultIndent, showBackAnimation) {
    var item_width = sliderPanel.find('li').outerWidth(true);

    /* same as for sliding right except that it's current left indent + the item width (for the sliding right it's - item_width) */
    var indent = parseInt(sliderPanel.css('right')) + item_width;

    if (showBackAnimation) {
        sliderPanel.animate({ 'right': indent - item_width - 90 });
    }

    sliderPanel.animate({ 'right': indent }, "slow", function () {
        /* when sliding to left we are moving the last item before the first item */
        sliderPanel.find('li:first').before(sliderPanel.find('li:last'));

        /* and again, when we make that change we are setting the left indent of our unordered list to the default indent*/
        sliderPanel.css({ 'right': '-' + defaultIndent + 'px' });
    });
}

function SliderMoveNext(sliderPanel, defaultIndent, showBackAnimation) {
    //get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '  
    var item_width = sliderPanel.find('li').outerWidth(true);

    //calculate the new left indent of the unordered list  
    var indent = parseInt(sliderPanel.css('right')) - item_width;

    if (showBackAnimation) {
        sliderPanel.animate({ 'right': indent + item_width + 90 });
    }

    //make the sliding effect using jquery's anumate function '  
    sliderPanel.animate({ 'right': indent }, "slow", function () {
        //get the first list item and put it after the last list item (that's how the infinite effects is made) '  
        sliderPanel.find('li:last').after(sliderPanel.find('li:first'));

        //and get the left indent to the default indent 
        sliderPanel.css({ 'right': '-' + defaultIndent + 'px' });
    });
}

//This function get "form" element (or "input" elements) and serialize the values into JSON object
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};